﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using System.Xml;

/// <summary>
/// 淘宝类目相关
/// </summary>
public class TB_ItemCat
{
    public TB_ItemCat()
    {       
    }
    /// <summary>
    /// 淘宝类目结构
    /// </summary>
    public struct ItemCat
    {
        public string cid;
        public string parent_cid;
        public string name;
        public bool is_parent;
        public string status;
        public int sort_order;
    }
    /// <summary>
    /// 下载淘宝类目
    /// </summary> 
    public List<ItemCat> DownTBItemCat(string username, string password, string appkey, string certcode, string sessionId, string parent_cid)
    {
        string[,] param = new string[,] { { "sip_apiname", "taobao.itemcats.get" }, {"parent_cid",parent_cid},
                    { "v", "1.0" }, { "sip_timestamp", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }, { "sip_format", "xml" },{"sip_appkey",appkey}};
        BS2.BSTaobaoAPI.TaobaoAPI.setAPPID(appkey);
        BS2.BSTaobaoAPI.TaobaoAPI.setCERTCODE(certcode);
        BS2.BSTaobaoAPI.TaobaoAPI.setLoginPro(username, password);
        BS2.BSTaobaoAPI.TaobaoAPI.setSessionID(sessionId);

        List<ItemCat> itemList = new List<ItemCat>();
        XmlDocument xml = null;
        try
        {
            xml = BS2.BSTaobaoAPI.TaobaoAPI.getXmlFromTaobao(param);

            if (xml == null||xml.OuterXml.Contains("error_rsp"))
                return null;
            foreach (XmlNode node in xml.SelectNodes("/rsp/item_cat"))
            {
                ItemCat item = new ItemCat();
                item.cid = node.SelectNodes("cid").Item(0).InnerText;
                item.parent_cid = node.SelectNodes("parent_cid").Item(0).InnerText;
                item.name = node.SelectNodes("name").Item(0).InnerText;
                item.is_parent = node.SelectNodes("is_parent").Item(0).InnerText == "true" ? true : false;
                itemList.Add(item);
            }           
        }
        catch
        {
            return null;
        }
        return itemList;
    }
    public struct TB_ItemProp
    {
        public string pid;
        public string name;
        public string parent_pid;
        public string parent_vid;
    }
    public struct TB_PropValue
    {
        public string cid;
        public string pid;
        public string prop_name;
        public string vid;
        public string name;
    }
    /// <summary>
    /// 下载淘宝类目属性
    /// </summary>    
    public void GetTb_ItemPropList(string username, string password, string appkey, string certcode, string sessionId, string cid)
    {
        string[,] param = new string[,] { { "sip_apiname", "taobao.itemprops.get" }, {"cid",cid},
                    { "v", "1.0" }, { "sip_timestamp", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") }, { "sip_format", "xml" },{"sip_appkey",appkey}};
        BS2.BSTaobaoAPI.TaobaoAPI.setAPPID(appkey);
        BS2.BSTaobaoAPI.TaobaoAPI.setCERTCODE(certcode);
        BS2.BSTaobaoAPI.TaobaoAPI.setLoginPro(username, password);
        BS2.BSTaobaoAPI.TaobaoAPI.setSessionID(sessionId);

        XmlDocument xml = BS2.BSTaobaoAPI.TaobaoAPI.getXmlFromTaobao(param);
        if (xml == null || xml.OuterXml.Contains("error_rsp"))
            return;

        foreach (XmlNode node in xml.SelectNodes("/rsp/item_prop"))
        {
            TB_ItemProp itemProp = new TB_ItemProp();
            itemProp.name = node.SelectNodes("name").Item(0).InnerText;
            itemProp.pid = node.SelectNodes("pid").Item(0).InnerText;

            //获取propvalue            
            foreach (XmlNode node2 in node.SelectNodes("prop_values/prop_value"))
            {
                TB_PropValue propValue = new TB_PropValue();
                propValue.vid = node2.SelectNodes("vid").Item(0).InnerText;
                propValue.name = node2.SelectNodes("name").Item(0).InnerText;
                // 插入表TB_SXLINK
                InsertIntoTB_SXLink(itemProp.name, propValue.vid, propValue.name);
                //插入表TB_GUIGE1
                if (itemProp.name.Contains("颜色"))
                    InsertIntoTB_GuiGe1(propValue.vid, propValue.name);
                //插入表TB_GUIGE2
                if (itemProp.name.Contains("尺码") || itemProp.name.Contains("尺寸"))
                    InsertIntoTB_GuiGe2(propValue.vid, propValue.name);
            }
        }
        //下载完成后，更改类目的fideld1为1，表示已经下载过类目属性
        int Mid = new TBCommon().GetTBLeiMuMID(cid);
        Model.TaoBao.TB_LEIMU m = new BLL.TaoBao.TB_LEIMU().GetModel(Mid);
        if (m != null)
        {
            m.field1 = "1";
            new BLL.TaoBao.TB_LEIMU().Update(m);
        }
    }
    /// <summary>
    /// 判断某类目是否没有子类目，即是否为最终子类目
    /// </summary>
    public bool CheckIsNoChild(string cid)
    {
        int Mid= new TBCommon().GetTBLeiMuMID(cid);
        string sql = "select count(*) from tb_leimu where parentid=" + Mid + "";
        if (DBUtility.SqlHelper.ExecuteCountSql(DBUtility.SqlHelper.LocalSqlServer, sql) > 0)
            return false;
        return true;        
    }
    void InsertIntoTB_SXLink(string LMSX, string TB_ID, string MC)
    {
        //判断重复
        string sql = "select count(*) from tb_sxlink where tb_id=" + Convert.ToInt32(TB_ID) + "";
        if (DBUtility.SqlHelper.ExecuteCountSql(DBUtility.SqlHelper.LocalSqlServer, sql) > 0)
            return;        
        Model.TaoBao.TB_SXLINK link = new Model.TaoBao.TB_SXLINK();
        link.LMSX = LMSX;
        link.LSNR = MC;
        link.TB_ID = Convert.ToInt32(TB_ID);
        new BLL.TaoBao.TB_SXLINK().Add(link);
    }
    //颜色插入淘宝表，然后和本地颜色库关联
    void InsertIntoTB_GuiGe1(string TB_ID, string GGMC)
    {        
        string sql = "select count(*) from tb_guige1 where tb_id='" + TB_ID + "'";
        if (DBUtility.SqlHelper.ExecuteCountSql(DBUtility.SqlHelper.LocalSqlServer, sql) > 0)
            return;
        Model.TaoBao.TB_GUIGE1 guige1 = new Model.TaoBao.TB_GUIGE1();
        try
        {            
            guige1.TB_ID = TB_ID;
            guige1.TB_MC = GGMC;
            new BLL.TaoBao.TB_GUIGE1().Add(guige1);
        }
        catch
        {
        }
    }
    void InsertIntoTB_GuiGe2(string TB_ID, string GGMC)
    {        
        string sql = "select count(*) from tb_guige2 where tb_id='" + TB_ID + "'";
        if (DBUtility.SqlHelper.ExecuteCountSql(DBUtility.SqlHelper.LocalSqlServer, sql) > 0)
            return;
        Model.TaoBao.TB_GUIGE2 guige2 = new Model.TaoBao.TB_GUIGE2();
        try
        {          
            guige2.TB_ID = TB_ID;
            guige2.TB_MC = GGMC;
            new BLL.TaoBao.TB_GUIGE2().Add(guige2);
        }
        catch
        {           
        }
    }
}
